{extend name='public/base' /}
{block name='content'}
{include file='public/content_header' /}
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <!-- 表单头部 -->
                <div class="box-header with-border">
                    <div class="btn-group">
                        <a class="btn flat btn-sm btn-default BackButton">
                            <i class="fa fa-arrow-left"></i>
                            返回
                        </a>
                    </div>
                </div>
                <!-- 表单 -->
                <form id="dataForm" class="form-horizontal dataForm" action="" method="post"
                      enctype="multipart/form-data">
                    <!-- 表单字段区域 -->
                    <div class="box-body">
                        <div class="form-group">
                            <label for="name" class="col-sm-2 control-label">名称</label>
                            <div class="col-sm-10 col-md-4">
                                <input id="name" name="name" value="{$data.name|default=''}" placeholder="请输入名称"
                                       type="text" class="form-control field-text">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="img" class="col-sm-2 control-label">图片</label>
                            <div class="col-sm-10 col-md-4">
                                <input id="img" name="img" placeholder="请上传图片" type="file"
                                       class="form-control field-image" data-initial-preview="{$data.img|default=''}">
                            </div>
                        </div>
                        <script>
                            $('#img').fileinput({
                                language: 'zh',
                                overwriteInitial: true,
                                browseLabel: '浏览',
                                initialPreviewAsData: true,
                                dropZoneEnabled: false,
                                showUpload: false,
                                showRemove: false,
                                allowedFileTypes: ['image'],
                                maxFileSize: 10240,
                            });
                        </script>
                        <div class="form-group">
                            <label for="jump_type" class="col-sm-2 control-label">跳转类型</label>
                            <div class="col-sm-10 col-md-4">
                                <select name="jump_type" id="jump_type" class="form-control field-select"
                                        data-placeholder="请选择跳转类型" onchange="getTargetList(this.value)">
                                    <option value=""></option>
                                    {foreach name='jump_type' id='item'}
                                    <option value="{$item.id}" {if isset($data) &&
                                            $data.jump_type==$item.id}selected{/if}>{$item.name}
                                    </option>
                                    {/foreach}

                                </select>
                            </div>
                        </div>
                        <script>
                            $('#jump_type').select2();
                        </script>
                        <div class="form-group">
                            <label for="jump_target" class="col-sm-2 control-label">跳转目标</label>
                            <div class="col-sm-10 col-md-4">
                                <select name="jump_target" id="jump_target" class="form-control field-select">
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#jump_target').select2({
                                placeholder: '请选择跳转目标'
                            });
                        </script>
                        <div class="form-group">
                            <label for="sort_number" class="col-sm-2 control-label">排序</label>
                            <div class="col-sm-10 col-md-4">
                                <input id="sort_number" name="sort_number" value="{$data.sort_number|default='1000'}"
                                       placeholder="请输入排序" type="text" class="form-control field-text">
                            </div>
                        </div>

                    </div>
                    <!-- 表单底部 -->
                    <div class="box-footer">
                        {:token()}
                        <div class="col-sm-2">
                        </div>
                        <div class="col-sm-10 col-md-4">
                            {if !isset($data)}
                            <div class="btn-group pull-right">
                                <label class="createContinue">
                                    <input type="checkbox" value="1" id="_create" name="_create"
                                           title="继续添加数据">继续添加</label>
                            </div>
                            {/if}
                            <div class="btn-group">
                                <button type="submit" class="btn flat btn-info dataFormSubmit">
                                    保存
                                </button>
                            </div>
                            <div class="btn-group">
                                <button type="reset" class="btn flat btn-default dataFormReset">
                                    重置
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<script>
    /** 表单验证 **/
    $('#dataForm').validate({
        rules: {
            'name': {
                required: true,
            },
            'jump_type': {
                required: true,
            },
            'jump_target': {
                required: true,
            },
            'sort_number': {
                required: true,
            },

        },
        messages: {
            'name': {
                required: "名称不能为空",
            },
            'jump_type': {
                required: "跳转类型不能为空",
            },
            'jump_target': {
                required: "跳转目标不能为空",
            },
            'sort_number': {
                required: "排序不能为空",
            },

        }
    });

    function getTargetList(id) {
        var html ='';
        var $jump_target =  $('#jump_target');
        id = parseInt(id);
        if (id !== 3) {
            $.post('{:url("pub/getSlideTargetData")}', {type_id: id}, function (result) {

                $.each(result.data,function (index, item) {
                    html+='<option value="'+item.id+'">'+item.name+'</option>';
                });

                $jump_target.html(html);
                $jump_target.select2();
            });
        }else{
            $jump_target.html(html);
            $jump_target.select2({
                tags:true,
                placeholder:'请输入需要跳转的网址',
            });
        }



    }
</script>
{/block}